#define _CRT_SECURE_NO_WARNINGS 1

#include "SList.h"

Listnode* Mergetwolist(Listnode* list1, Listnode* list2)
{
	Listnode* l1 = list1;
	Listnode* l2 = list2;

	Listnode* pos = (Listnode*)malloc(sizeof(Listnode));
	if (pos == NULL)
	{
		perror("malloc");
		exit(1);
	}

	if (l1 == NULL && l2 == NULL)
		return list1;
	pos->next = NULL;
	Listnode* pcur = pos;
	while (l1 && l2)
	{
		if (l1->data <= l2->data)
		{
			pcur->next = l1;
			pcur = l1;
			l1 = l1->next;
		}
		else
		{
			pcur->next = l2;
			pcur = l2;
			l2 = l2->next;
		}
	}
	if (l1)
		pcur->next = l1;
	if (l2)
		pcur->next = l2;
	Listnode* ret = pos->next;
	return ret;
}

int main()
{
	Listnode* phead1 = NULL;
	Listnode* phead2 = NULL;

	Listpushback(&phead1, 1);
	Listpushback(&phead1, 3);
	Listpushback(&phead1, 4);
	Listpushback(&phead2, 1);
	Listpushback(&phead2, 2);
	Listpushback(&phead2, 6);
	
	Listnode* nlist = Mergetwolist(phead1, phead2);
	Listprint(nlist);
	return 0;
}